import hashlib
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from random import Random
import logging


def get_md5(src):
    m2 = hashlib.md5()
    m2.update(src.encode('utf8'))
    return m2.hexdigest()


def random_str(randomlength=8):
    string = ''
    chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    length = len(chars) - 1
    random = Random()
    for i in range(randomlength):
        string += chars[random.randint(0, length-1)]
    return string


def send_mail(subject="test", content=''):
    sender = 'bo.ye@jifurizhi.com'
    receivers = ['19744209@qq.com', 'xu.qian@jifurizhi.com']

    message = MIMEText(content, 'plain', 'utf-8')
    message['From'] = Header("肌肤日志<bo.ye@jifurizhi.com>", 'utf-8')
    message['To'] = Header("管理员", 'utf-8')
    message['Subject'] = Header(subject, 'utf-8')

    try:
        smtp_obj = smtplib.SMTP()
        smtp_obj.connect('smtp.mxhichina.com', 25)
        smtp_obj.login('bo.ye@jifurizhi.com', 'asdf1234~')
        smtp_obj.sendmail(sender, receivers, message.as_string())
    except smtplib.SMTPException as e:
        # print("Error: 无法发送邮件")
        logging.info(e)